<?php
class MaYi{
	var $sock;
        var $domain;
	function __construct($port = 7080, $dm ="o" ){
                $this->domain = $dm;
		$this->sock = fsockopen("localhost", $port, $errno, $errstr);
		if (!$this->sock) 
			syslog("ERROR: $errno - $errstr");
	} 

	function __destruct() {
               fclose($this->sock);
	} 

	function domethod2($num ){
		$input = "$num";
		$sinput = "x".pack("I", strlen($input))."yz".$input;
//		$winput = "x".pack("I", strlen($pinput))."yz".$sinput;
                $wlen =  fwrite($this->sock, $sinput);
                 var_dump($wlen);
     	        $readlen = $READ = 102400;
		$response = fread($this->sock, $READ);
                var_dump(strlen($response)); 
           return true;                
	}

	function domethod($num ){

		$input = "$num";
		$sinput = "x".pack("I", strlen($input))."yz".$input;
		$pinput = "P".pack("I", 1 )."\001".$sinput;
		$winput = "x".pack("I", strlen($pinput))."yz".$pinput;
                fwrite($this->sock, $winput);
     	        $readlen = $READ = 102400;
		$response = fread($this->sock, $READ);

		if(isset($response{7}))
		{
			$sub = substr($response, 0, 8);
			$unpacked = unpack('I2',$sub);
var_dump($unpacked);
			if(isset($unpacked['1']))
			{
				$left = $unpacked['1'] - strlen($response);
				while ( $left > 0 && !feof($this->sock) )
				{
					$contstr = fread( $this->sock, $READ );
					if ( $contstr )
					{
						$response .= $contstr;
						$left -= strlen($contstr);
					}
					else
						return false;
				}
                          }
                 }
                var_dump(strlen($response)); 
           return true;                
	}
}


$obj = new MaYi(7080);
$ret = $obj->domethod("1020");
var_dump($ret);
//$ret = $obj->domethod("109");
//var_dump($ret);

exit;


//$obj = new MaYi(11234);
$obj->domethod2("102400");
//sleep(4);
//$obj->domethod2("199");

?>
